#if !defined(__OV9712_CMOS_H_)
#define __OV9712_CMOS_H_

#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "hi_comm_sns.h"
#include "hi_comm_video.h"
#include "hi_sns_ctrl.h"
#include "mpi_isp.h"
#include "mpi_ae.h"
#include "mpi_awb.h"
#include "mpi_af.h"

#ifdef __cplusplus
#if __cplusplus
extern "C"{
#endif
#endif /* End of #ifdef __cplusplus */


#define OV9712_ID 9712


/* To change the mode of config. ifndef INIFILE_CONFIG_MODE, quick config mode.*/
/* else, cmos_cfg.ini file config mode*/
#ifdef INIFILE_CONFIG_MODE

extern AE_SENSOR_DEFAULT_S  g_AeDft[];
extern AWB_SENSOR_DEFAULT_S g_AwbDft[];
extern ISP_CMOS_DEFAULT_S   g_IspDft[];
extern HI_S32 Cmos_LoadINIPara(const HI_CHAR *pcName);
#else

#endif

/****************************************************************************
 * local variables                                                            *
 ****************************************************************************/

extern const unsigned int sensor_i2c_addr;
extern unsigned int sensor_addr_byte;
extern unsigned int sensor_data_byte;

#define FULL_LINES_MAX  (0xFFFF)

#define VMAX_ADDR_H              (0x2e)
#define VMAX_ADDR_L              (0x2d)

#define SENSOR_720P_30FPS_MODE  (1)

#define INCREASE_LINES (0) /* make real fps less than stand fps because NVR require*/
#define VMAX_720P30_LINEAR     (810+INCREASE_LINES)
#define CMOS_OV9712_SLOW_FRAMERATE_MODE (0)

HI_U8 gu8SensorImageMode = SENSOR_720P_30FPS_MODE;
WDR_MODE_E genSensorMode = WDR_MODE_NONE;

static HI_U32 gu32FullLinesStd = VMAX_720P30_LINEAR;
static HI_U32 gu32FullLines = VMAX_720P30_LINEAR;

static HI_BOOL bInit = HI_FALSE;
HI_BOOL bSensorInit = HI_FALSE;
ISP_SNS_REGS_INFO_S g_stSnsRegsInfo = {0};
ISP_SNS_REGS_INFO_S g_stPreSnsRegsInfo = {0};
static HI_U8 gu8Fps = 30;


#define PATHLEN_MAX 256
#define CMOS_CFG_INI "ov9712_cfg.ini"
static char pcName[PATHLEN_MAX] = "configs/ov9712_cfg.ini";

/* Piris attr */
static ISP_PIRIS_ATTR_S gstPirisAttr=
{
    0,      // bStepFNOTableChange
    1,      // bZeroIsMax
    94,     // u16TotalStep
    62,     // u16StepCount
    /* Step-F number mapping table. Must be from small to large. F1.0 is 1024 and F32.0 is 1 */
    {30,35,40,45,50,56,61,67,73,79,85,92,98,105,112,120,127,135,143,150,158,166,174,183,191,200,208,217,225,234,243,252,261,270,279,289,298,307,316,325,335,344,353,362,372,381,390,399,408,417,426,435,444,453,462,470,478,486,493,500,506,512},
    ISP_IRIS_F_NO_1_4, // enMaxIrisFNOTarget
    ISP_IRIS_F_NO_5_6  // enMinIrisFNOTarget
};


/* AE default parameter and function */
static HI_S32 cmos_get_ae_default(AE_SENSOR_DEFAULT_S *pstAeSnsDft)
{
    if (HI_NULL == pstAeSnsDft)
    {
        printf("null pointer when get ae default value!\n");
        return -1;
    }

    pstAeSnsDft->u32LinesPer500ms = gu32FullLinesStd*25/2;
    pstAeSnsDft->u32FullLinesStd = gu32FullLinesStd;
    pstAeSnsDft->u32FlickerFreq = 0;
    pstAeSnsDft->u32FullLinesMax = FULL_LINES_MAX;

    pstAeSnsDft->au8HistThresh[0] = 0xd;
    pstAeSnsDft->au8HistThresh[1] = 0x28;
    pstAeSnsDft->au8HistThresh[2] = 0x60;
    pstAeSnsDft->au8HistThresh[3] = 0x80;
            
    pstAeSnsDft->u8AeCompensation = 0x38;

    pstAeSnsDft->stIntTimeAccu.enAccuType = AE_ACCURACY_LINEAR;
    pstAeSnsDft->stIntTimeAccu.f32Accuracy = 1;
    pstAeSnsDft->stIntTimeAccu.f32Offset = 0;
    pstAeSnsDft->u32MaxIntTime = gu32FullLinesStd - 2;//806
    pstAeSnsDft->u32MinIntTime = 1;
    pstAeSnsDft->u32MaxIntTimeTarget = 65535;
    pstAeSnsDft->u32MinIntTimeTarget = 1;

    pstAeSnsDft->stAgainAccu.enAccuType = AE_ACCURACY_DB;
    pstAeSnsDft->stAgainAccu.f32Accuracy = 6;
    pstAeSnsDft->u32MaxAgain = 4;  /* 1, 2, 4, ... 16 (0~24db, unit is 6db) */
    pstAeSnsDft->u32MinAgain = 0;
    pstAeSnsDft->u32MaxAgainTarget = 4;
    pstAeSnsDft->u32MinAgainTarget = 0;
        
    pstAeSnsDft->stDgainAccu.enAccuType = AE_ACCURACY_LINEAR;
    pstAeSnsDft->stDgainAccu.f32Accuracy = 0.0625;
    pstAeSnsDft->u32MaxDgain = 31;  /* 1 ~ 31/16, unit is 1/16 */
    pstAeSnsDft->u32MinDgain = 16;
    pstAeSnsDft->u32MaxDgainTarget = 32;
    pstAeSnsDft->u32MinDgainTarget = 16; 
    
    pstAeSnsDft->u32ISPDgainShift = 8;
    pstAeSnsDft->u32MinISPDgainTarget = 1 << pstAeSnsDft->u32ISPDgainShift;
    pstAeSnsDft->u32MaxISPDgainTarget = 4 << pstAeSnsDft->u32ISPDgainShift; 
	  
    pstAeSnsDft->u32LinesPer500ms = gu32FullLinesStd*30/2;

    pstAeSnsDft->enIrisType = ISP_IRIS_DC_TYPE;
    memcpy(&pstAeSnsDft->stPirisAttr, &gstPirisAttr, sizeof(ISP_PIRIS_ATTR_S));
    pstAeSnsDft->enMaxIrisFNO = ISP_IRIS_F_NO_1_4;
    pstAeSnsDft->enMinIrisFNO = ISP_IRIS_F_NO_5_6;

    /*For some OV sensors, AERunInterval needs to be set more than 1*/
    pstAeSnsDft->u8AERunInterval = 2;

    return 0;
}

/* the function of sensor set fps */
static HI_VOID cmos_fps_set(HI_FLOAT f32Fps, AE_SENSOR_DEFAULT_S *pstAeSnsDft)
{
    HI_U32 tp = 1692;
    HI_U32 u32VblankingLines = FULL_LINES_MAX;
    if ((f32Fps <= 30) && (f32Fps >= 6.5))
    {
        if(SENSOR_720P_30FPS_MODE == gu8SensorImageMode)
        {
            u32VblankingLines = tp * 30 / f32Fps + 3;
        }
    }
    else
    {
        printf("Not support Fps: %f\n", f32Fps);
        return;
    }

    g_stSnsRegsInfo.astI2cData[3].u32Data = u32VblankingLines & 0xfc;
    g_stSnsRegsInfo.astI2cData[4].u32Data = (u32VblankingLines & 0xff00) >> 8;
    
    pstAeSnsDft->f32Fps = f32Fps;
    pstAeSnsDft->u32MaxIntTime = 806;
    gu32FullLinesStd = VMAX_720P30_LINEAR;
    gu8Fps = f32Fps;
    pstAeSnsDft->u32LinesPer500ms = gu32FullLinesStd * f32Fps / 2;
    pstAeSnsDft->u32FullLinesStd = gu32FullLinesStd;
    gu32FullLines = gu32FullLinesStd;
    pstAeSnsDft->u32FullLines = gu32FullLines;

    return;
}

static HI_VOID cmos_slow_framerate_set(HI_U32 u32FullLines,
    AE_SENSOR_DEFAULT_S *pstAeSnsDft)
{
/*Mode 1: slow framerate by add the time of each line*/
#if CMOS_OV9712_SLOW_FRAMERATE_MODE
    HI_U32 u32Tp = 50760;   /* (0x69c * 30) = 50760*/
    HI_U16 u16SlowFrameRate = (u32FullLines << 8) / gu32FullLinesStd;
    u32Tp = (((u32Tp * u16SlowFrameRate) / gu8Fps) >> 8) + 3;
    if (u32Tp > 0x2000)     /* the register 0x2a adn 0x2b's max value is 0x2000 */
    {
        u32Tp = 0x2000;
        u16SlowFrameRate = ((gu8Fps * u32Tp) << 8) / 50760;
        printf("Warning! The slow_framerate is out of ov9712's range!\n");
    }

    pstAeSnsDft->u32LinesPer500ms = gu32FullLines * (gu8Fps << 8) / (2*u16SlowFrameRate);
    pstAeSnsDft->u32MaxIntTime = ((gu32FullLines * u16SlowFrameRate) >> 8) - 4;
    pstAeSnsDft->u32FullLines = gu32FullLines; 

    g_stSnsRegsInfo.astI2cData[3].u32Data = u32Tp & 0xfc;
    g_stSnsRegsInfo.astI2cData[4].u32Data = (u32Tp & 0xff00) >> 8;

#else
/*Mode 2:slow framerate by add the lines of each time*/
    HI_U32 u32VblankingLines;
    static HI_U32 u32LastVblankingLines = 0;

    u32FullLines = (u32FullLines > FULL_LINES_MAX) ? FULL_LINES_MAX : u32FullLines;
    gu32FullLines = u32FullLines;
    pstAeSnsDft->u32FullLines = gu32FullLines;

    u32VblankingLines = gu32FullLines - gu32FullLinesStd;
    /*avoid flicker in slow frame rate*/
    if ( u32LastVblankingLines < u32VblankingLines)
    {
        g_stSnsRegsInfo.astI2cData[5].u8DelayFrmNum = 0;
        g_stSnsRegsInfo.astI2cData[6].u8DelayFrmNum = 0;
    }
    else
    {
        g_stSnsRegsInfo.astI2cData[5].u8DelayFrmNum = 1;
        g_stSnsRegsInfo.astI2cData[6].u8DelayFrmNum = 1;
    }
    g_stSnsRegsInfo.astI2cData[5].u32Data = u32VblankingLines & 0xFF;
    g_stSnsRegsInfo.astI2cData[6].u32Data = (u32VblankingLines & 0xFF00) >> 8;

    u32LastVblankingLines = u32VblankingLines;
    
    pstAeSnsDft->u32MaxIntTime = gu32FullLines - 4;
#endif 
    return;
}

/* while isp notify ae to update sensor regs, ae call these funcs. */
static HI_VOID cmos_inttime_update(HI_U32 u32IntTime)
{

    g_stSnsRegsInfo.astI2cData[0].u32Data = u32IntTime & 0xFF;
    g_stSnsRegsInfo.astI2cData[1].u32Data = (u32IntTime >> 8) & 0xFF;  

    return;
}

static HI_VOID cmos_again_calc_table(HI_U32 *pu32AgainLin, HI_U32 *pu32AgainDb)
{
   
    *pu32AgainDb = (*pu32AgainLin >> 3);

    return;
}

static HI_VOID cmos_dgain_calc_table(HI_U32 *pu32AgainLin, HI_U32 *pu32AgainDb)
{
    //*pu32AgainLin = (*pu32AgainLin >> 10) << 10;
    
    return;
}

static HI_VOID cmos_gains_update(HI_U32 u32Again, HI_U32 u32Dgain)
{  
   HI_U8 u8High, u8Low;
    switch (u32Again)
    {
        case 0 :    /* 0db, 1 multiplies */
            u8High = 0x00;
            break;
        case 1 :    /* 6db, 2 multiplies */
            u8High = 0x10;
            break;
        case 2 :    /* 12db, 4 multiplies */
            u8High = 0x30;
            break;
        case 3 :    /* 18db, 8 multiplies */
            u8High = 0x70;
            break;
        case 4 :    /* 24db, 16 multiplies */
            u8High = 0xf0;
            break;
        default:
            u8High = 0x00;
            break;
    }

    u8Low = (u32Dgain - 16) & 0xf;
    g_stSnsRegsInfo.astI2cData[2].u32Data = (u8High | u8Low);
    
    return;
}

HI_S32 cmos_init_ae_exp_function(AE_SENSOR_EXP_FUNC_S *pstExpFuncs)
{
    memset(pstExpFuncs, 0, sizeof(AE_SENSOR_EXP_FUNC_S));

    pstExpFuncs->pfn_cmos_get_ae_default    = cmos_get_ae_default;
    pstExpFuncs->pfn_cmos_fps_set           = cmos_fps_set;
    pstExpFuncs->pfn_cmos_slow_framerate_set= cmos_slow_framerate_set;    
    pstExpFuncs->pfn_cmos_inttime_update    = cmos_inttime_update;
    pstExpFuncs->pfn_cmos_gains_update      = cmos_gains_update;
    pstExpFuncs->pfn_cmos_again_calc_table  = cmos_again_calc_table;
    pstExpFuncs->pfn_cmos_dgain_calc_table  = cmos_dgain_calc_table;

    return 0;
}


/* AWB default parameter and function */
static AWB_CCM_S g_stAwbCcm =
{  
#if 0
    5120,    
    {       
        0x01c8,  0x809b,  0x802d,
        0x8058,  0x01a3,  0x804b,       
        0x800b,  0x80fb,  0x0206    
    },  
    
    3633,    
    {       
        0x01b7,  0x8083,  0x8034,       
        0x805f,  0x018b,  0x802c,       
        0x8016,  0x812f,  0x0245    
    },
    
    2465,    
    {            
        0x0212,  0x80d6,  0x803c,        
        0x806d,  0x017c,  0x800f,       
        0x8027,  0x8164,  0x028b    
    }
#endif
//#if 1
    5120,    
    {       
        0x0270,  0x812b,  0x8045,
        0x8048,  0x01c5,  0x807d,       
        0x0011,  0x818e,  0x027d    
    },  
    
    3633,    
    {       
        0x01cf,  0x8089,  0x8046,       
        0x8059,  0x018e,  0x8035,       
        0x8016,  0x812f,  0x0245    
    },
    
    2465,    
    {            
        0x0205,  0x80a2,  0x8063,        
        0x8058,  0x0189,  0x8031,       
        0x802e,  0x8175,  0x02a3    
    }
//#endif
};

static AWB_AGC_TABLE_S g_stAwbAgcTable =
{
    /* bvalid */
    1,
	
    /*1,  2,  4,  8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768*/
    /* saturation */   
    {0x80,0x78,0x70,0x68,0x60,0x55,0x4b,0x40,0x38,0x38,0x38,0x38,0x38,0x38,0x38,0x38}

};

static HI_S32 cmos_get_awb_default(AWB_SENSOR_DEFAULT_S *pstAwbSnsDft)
{
    if (HI_NULL == pstAwbSnsDft)
    {
        printf("null pointer when get awb default value!\n");
        return -1;
    }

    memset(pstAwbSnsDft, 0, sizeof(AWB_SENSOR_DEFAULT_S));

    pstAwbSnsDft->u16WbRefTemp = 5120;
    pstAwbSnsDft->au16GainOffset[0] = 0x17e;    
    pstAwbSnsDft->au16GainOffset[1] = 0x100;    
    pstAwbSnsDft->au16GainOffset[2] = 0x100;    
    pstAwbSnsDft->au16GainOffset[3] = 0x179;    
    pstAwbSnsDft->as32WbPara[0] = 95;    
    pstAwbSnsDft->as32WbPara[1] = 45;    
    pstAwbSnsDft->as32WbPara[2] = -116;    
    pstAwbSnsDft->as32WbPara[3] = 182769;    
    pstAwbSnsDft->as32WbPara[4] = 128;    
    pstAwbSnsDft->as32WbPara[5] = -132156;
    
    memcpy(&pstAwbSnsDft->stCcm, &g_stAwbCcm, sizeof(AWB_CCM_S));
    memcpy(&pstAwbSnsDft->stAgcTbl, &g_stAwbAgcTable, sizeof(AWB_AGC_TABLE_S));
    
    return 0;
}

HI_S32 cmos_init_awb_exp_function(AWB_SENSOR_EXP_FUNC_S *pstExpFuncs)
{
    memset(pstExpFuncs, 0, sizeof(AWB_SENSOR_EXP_FUNC_S));

    pstExpFuncs->pfn_cmos_get_awb_default = cmos_get_awb_default;

    return 0;
}

#define DMNR_CALIB_CARVE_NUM_OV9712 11

float g_coef_calib_ov9712[DMNR_CALIB_CARVE_NUM_OV9712][4] = 
{
    {100.000000f, 2.000000f, 0.038318f, 8.825197f, }, 
    {200.000000f, 2.301030f, 0.044073f, 8.286962f, }, 
    {400.000000f, 2.602060f, 0.044098f, 9.092120f, }, 
    {800.000000f, 2.903090f, 0.047732f, 10.356674f, }, 
    {1600.000000f, 3.204120f, 0.051677f, 13.629107f, }, 
    {3300.000000f, 3.518514f, 0.058334f, 19.530838f, }, 
    {5600.000000f, 3.748188f, 0.072166f, 25.358517f, }, 
    {7100.000000f, 3.851258f, 0.082855f, 36.527088f, }, 
    {11300.000000f, 4.053079f, 0.097958f, 43.649172f, },
    {12300.000000f, 4.089905f, 0.097744f, 58.210743f, }, 
    {24600.000000f, 4.390935f, 0.096482f, 80.523537f, },
};


static ISP_NR_ISO_PARA_TABLE_S g_stNrIsoParaTab[HI_ISP_NR_ISO_LEVEL_MAX] = 
{
     //u16Threshold//u8varStrength//u8fixStrength//u8LowFreqSlope	
       {1500,       160,             256-256,            0 },  //100    //                      //                                                
       {1500,       120,             256-256,            0 },  //200    // ISO                  // ISO //u8LowFreqSlope
       {1500,       100,             256-256,            0 },  //400    //{400,  1200, 96,256}, //{400 , 0  }
       {1750,       80,              256-256,            8 },  //800    //{800,  1400, 80,256}, //{600 , 2  }
       {1500,       255,             256-256,            6 },  //1600   //{1600, 1200, 72,256}, //{800 , 8  }
       {1500,       255,             256-256,            0 },  //3200   //{3200, 1200, 64,256}, //{1000, 12 }
       {1375,       255,             256-256,            0 },  //6400   //{6400, 1100, 56,256}, //{1600, 6  }
       {1375,       255,             256-256,            0 },  //12800  //{12000,1100, 48,256}, //{2400, 0  }
       {1375,       255,             256-256,            0 },  //25600  //{36000,1100, 48,256}, //
       {1375,       255,             256-256,            0 },  //51200  //{64000,1100, 96,256}, //
       {1250,       255,             256-256,            0 },  //102400 //{82000,1000,240,256}, //
       {1250,       255,             256-256,            0 },  //204800 //                           //
       {1250,       255,             256-256,            0 },  //409600 //                           //
       {1250,       255,             256-256,            0 },  //819200 //                           //
       {1250,       255,             256-256,            0 },  //1638400//                           //
       {1250,       255,             256-256,            0 },  //3276800//                           //
};

static ISP_CMOS_DEMOSAIC_S g_stIspDemosaic =
{
	/*For Demosaic*/
	1, /*bEnable*/			
	12,/*u16VhLimit*/	
	8,/*u16VhOffset*/
	48,   /*u16VhSlope*/
	/*False Color*/
	1,    /*bFcrEnable*/
	{ 8, 8, 8, 8, 8, 8, 8, 8, 3, 0, 0, 0, 0, 0, 0, 0},    /*au8FcrStrength[ISP_AUTO_ISO_STENGTH_NUM]*/
	{24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24},    /*au8FcrThreshold[ISP_AUTO_ISO_STENGTH_NUM]*/
	/*For Ahd*/
	400, /*u16UuSlope*/	
	{512,512,512,512,512,512,512,  400,  0,0,0,0,0,0,0,0}    /*au16NpOffset[ISP_AUTO_ISO_STENGTH_NUM]*/
};

static ISP_CMOS_GE_S g_stIspGe =
{
	/*For GE*/
	0,    /*bEnable*/			
	7,    /*u8Slope*/	
	7,    /*u8Sensitivity*/
	4096, /*u16Threshold*/
	4096, /*u16SensiThreshold*/	
	{1024,1024,1024,2048,2048,2048,2048,  2048,  2048,2048,2048,2048,2048,2048,2048,2048}    /*au16Strength[ISP_AUTO_ISO_STENGTH_NUM]*/	
};
#if 0
static ISP_CMOS_RGBSHARPEN_S g_stIspRgbSharpen =
{      
  //{100,200,400,800,1600,3200,6400,12800,25600,51200,102400,204800,409600,819200,1638400,3276800};
    {0,	  0,   0,  0,  0,   1,   1,    1,    1,    1,    1,     1,     1,     1,     1,       1},/* enPixSel = ~bEnLowLumaShoot */
    {30, 30, 32, 35,  35,  60,  100,  120,    160,  200,  250,   250,   250,   250,    250,    250},/*maxSharpAmt1 = SharpenUD*16 */
    {80, 80, 100, 140,  180,  200,  250,  250,    250,  250,  250,   250,   250,   250,    250,    250},/*maxEdgeAmt = SharpenD*16 */
    {0,  0,   0,  0,   0,  0,  20,   40,    90,    120,    180,     250,    250,     250,     250,       250},/*sharpThd2 = TextureNoiseThd*4 */
    {0,  0,   0,  0,   0,  0,  0,   0,    0,    0,    0,     0,    0,     0,     0,       0},/*edgeThd2 = EdgeNoiseThd*4 */
    {255, 220, 200, 160, 120,  80, 30,   20,  0,  0,   0,    0,   0,    0,    0,      0},/*overshootAmt*/
    {255, 255, 220, 200, 160,  130,100,  70,  30,  0,   0,    0,   0,    0,    0,     0},/*undershootAmt*/
};
#else
static ISP_CMOS_RGBSHARPEN_S g_stIspRgbSharpen =
{      
  //{100, 200, 400,  800,1600,3200,6400,12800,25600,51200,102400,204800,409600,819200,1638400,3276800};
    {0,	    0,   0,   0,   0,   1,   1,    1,    1,    1,    1,     1,     1,     1,     1,       1},/*bEnLowLumaShoot */
        
    {50,   50,  53,  55,  55,  55,  60,   60,   65,   70,   70,    75,    75,    75,    75,      75},/*SharpenUD*16 */
    {65,   65,  70,  75,  80,  85,  90,   90,   90,   95,   95,    95,    95,    95,    95,      95},/*SharpenD*16 */
        
    {0,     0,   0,   0,   0,   0,   0,    0,    0,    0,    0,     0,     0,     0,     0,       0},/*TextureNoiseThd*4 */
    {0,     0,   0,   0,   0,   0,   0,    0,    0,    0,    0,     0,     0,     0,     0,       0},/*EdgeNoiseThd*4 */
    {75,   78,  80,  83,  85,  90,  88,   85,   80,   75,   70,    65,    60,    60,    60,      60},/*overshootAmt*/
    {120, 110, 100,  95,  95,  90,  88,   85,   80,   75,   75,    70,    70,    68,    65,      65},/*undershootAmt*/
};

#endif

static ISP_CMOS_UVNR_S g_stIspUVNR = 
{
  //{100,	200,	400,	800,	1600,	3200,	6400,	12800,	25600,	51200,	102400,	204800,	409600,	819200,	1638400,	3276800};
	{1,	    2,      4,      5,      7,      48,     32,     16,     16,     16,      16,     16,     16,     16,     16,        16},      /*UVNRThreshold*/
 	{0,		0,		0,		0,		0,		0,		0,		0,		0,		1,			1,		2,		2,		2,		2,		2},  /*Coring_lutLimit*/
	{0,		0,		0,		16,		34,		34,		34,		34,		34,		34,		34,		34,		34,		34,		34,			34}  /*UVNR_blendRatio*/
};

static ISP_CMOS_DPC_S g_stCmosDpc = 
{
	//1,/*IR_channel*/
	//1,/*IR_position*/
	{70,150,240,248,250,252,252,252,252,252,252,252,252,252,252,252},/*au16Strength[16]*/
	{0,0,0,0,0,0,0,0,0x24,0x80,0x80,0x80,0xE5,0xE5,0xE5,0xE5},/*au16BlendRatio[16]*/
};

static ISP_LSC_CABLI_UNI_TABLE_S g_stCmosLscUniTable = 
{
   /*Mesh Grid Info: Width*/
   { 9, 13, 18, 26, 36, 50,  70,  98}, 
   /*Mesh Grid Info: Height*/
   { 6,  8, 10, 14, 20, 28,  39,  55},
};
static ISP_LSC_CABLI_TABLE_S g_stCmosLscTable[HI_ISP_LSC_LIGHT_NUM] = 
{
	/*Light Source One*/
	{   
	    /*AWB RGain*/
        531,
        /*AWB BGain*/
        668,
        /*Channel R*/
        {
          11115904,   11157695,   11260216,   11348563,   11521809,   11640839,   11575170,   11226863,   11057837,   11351025,   11712442,   11746682,   11606037,   11438035,   11289688,   11217863,   11187308,   
          11128393,   11173909,   11274352,   11359328,   11512661,   11624029,   11578790,   11207625,   11018201,   11323735,   11711986,   11758906,   11611300,   11434937,   11268915,   11216556,   11192250,   
          11134055,   11191366,   11258320,   11367483,   11540648,   11645401,   11569606,   11160230,   10940620,   11270445,   11708339,   11779393,   11615054,   11468173,   11284395,   11230059,   11205694,   
          11197718,   11233466,   11285612,   11398060,   11589997,   11666230,   11542641,   11109080,   10835952,   11178423,   11686849,   11815767,   11637068,   11500668,   11329972,   11277363,   11259373,   
          11253740,   11277997,   11360883,   11433851,   11590611,   11619540,   11473749,   10990880,   10689221,   11065921,   11609788,   11791917,   11685942,   11516737,   11405838,   11344658,   11313834,   
          11306234,   11322776,   11384420,   11506056,   11643711,   11641121,   11358578,   10795272,   10481881,   10904832,   11526058,   11756264,   11712442,   11569606,   11470414,   11377747,   11351828,  
          11392557,   11409514,   11453220,   11561215,   11611356,   11550285,   11173494,   10570729,   10256622,   10643841,   11364477,   11711017,   11746395,   11637800,   11565214,   11484480,   11439611,   
          11459764,   11487879,   11537108,   11624422,   11634481,   11495231,   11004398,   10364478,   10094915,   10408313,   11159816,   11622233,   11743759,   11670133,   11602288,   11541312,   11512441,   
          11541368,   11555386,   11606596,   11643711,   11590723,   11401407,   10915712,   10275538,   10000000,   10249546,   11013562,   11545188,   11704581,   11745650,   11634481,   11570051,   11570997,   
          11361419,   11421046,   11522140,   11579458,   11586315,   11461565,   10992436,   10338923,   10052289,   10320303,   11046672,   11523133,   11630882,   11552391,   11523133,   11457691,   11446085,   
          11398923,   11406162,   11470086,   11490127,   11596307,   11526776,   11166340,   10551636,   10199510,   10538285,   11198916,   11553556,   11608780,   11526500,   11431353,   11368879,   11334454,   
          11357881,   11336269,   11368557,   11483001,   11598039,   11663064,   11338138,   10767214,   10367782,   10746541,   11318516,   11550839,   11526334,   11440916,   11327785,   11275408,   11254845,  
          11288047,   11292813,   11342092,   11503362,   11647148,   11695709,   11490730,   10971641,   10549509,   10942013,   11461565,   11629590,   11519879,   11418283,   11309794,   11250847,   11231579,   
          11220164,   11251162,   11298486,   11499130,   11614774,   11706346,   11554554,   11091111,   10675046,   11059158,   11486946,   11609172,   11490456,   11344872,   11225921,   11143386,   11117649,   
          11189441,   11224717,   11295092,   11426740,   11605309,   11681291,   11596866,   11148599,   10781056,   11127878,   11514478,   11597145,   11457691,   11304588,   11201157,   11123507,   11100421,   
          11173494,   11205747,   11295357,   11408487,   11611748,   11650926,   11608332,   11193864,   10853445,   11171730,   11521478,   11597257,   11437165,   11287041,   11181435,   11104877,   11075443,   
          11151337,   11185177,   11296364,   11384204,   11605925,   11654819,   11623749,   11222623,   10888995,   11169294,   11543194,   11579570,   11455673,   11280693,   11162508,   11119447,   11082532,          
           
        }, 
        /*Channel Gr*/
        {   
          10949500,   10966598,   11014185,   11103847,   11229978,   11370942,   11401743,   11139318,   10983219,   11200096,   11441816,   11424928,   11217634,   11007977,   10900616,   10776113,   10739776,   
          10947151,   10960300,   11020982,   11119798,   11260831,   11404292,   11401483,   11121183,   10963761,   11179913,   11440560,   11440717,   11219597,   11021419,   10887657,   10770218,   10735027,   
          10927678,   10952091,   11026377,   11134109,   11288133,   11411214,   11394417,   11092762,   10901329,   11125884,   11426808,   11459384,   11242299,   11047229,   10921806,   10810827,   10785876,   
          10941403,   10985921,   11081307,   11154824,   11337837,   11407622,   11361021,   11039863,   10804753,   11037475,   11420284,   11480801,   11293130,   11099706,   10960445,   10855984,   10815082,   
          11022634,   11042545,   11109274,   11219698,   11347717,   11406425,   11299356,   10923524,   10632989,   10902898,   11363293,   11467636,   11318231,   11194730,   11015932,   10900949,   10832936,   
          11095273,   11114805,   11168726,   11247053,   11357718,   11405228,   11198340,   10718505,   10436848,   10769707,   11288286,   11438466,   11361176,   11221208,   11097785,   10973048,   10916370,   
          11179363,   11197438,   11242653,   11320178,   11392444,   11325204,   11047082,   10550386,   10212148,   10524007,   11151789,   11397066,   11398832,   11314441,   11166182,   11056413,   11011808,   
          11228717,   11252013,   11319153,   11370942,   11404812,   11314851,   10908273,   10349620,   10060073,   10304916,   10965588,   11352200,   11453242,   11339894,   11238257,   11114311,   11051135,   
          11298590,   11315107,   11371717,   11427383,   11415330,   11286044,   10859614,   10304703,   10000000,   10173421,   10865939,   11294405,   11449466,   11404656,   11306715,   11206120,   11157761,   
          11216577,   11248723,   11301757,   11352973,   11429890,   11316541,   10951563,   10387459,   10062421,   10252475,   10919850,   11291141,   11358492,   11286706,   11176165,   11095618,   11061990,   
          11157114,   11176515,   11221208,   11296651,   11426443,   11393378,   11124845,   10562286,   10185523,   10445437,   11037475,   11311011,   11307379,   11181413,   11135002,   11031047,   10985101,   
          11062332,   11081602,   11154475,   11266261,   11410537,   11508015,   11261085,   10757243,   10345980,   10664920,   11163390,   11320999,   11272407,   11148855,   11029344,   10930353,   10885809,   
          11001535,   11013990,   11094141,   11248875,   11427905,   11525681,   11418823,   10949500,   10560323,   10830495,   11258752,   11375339,   11246547,   11117622,   10985680,   10886235,   10870190,   
          10961261,   10969822,   11051477,   11241996,   11400963,   11536850,   11468162,   11053578,   10669562,   10927296,   11314031,   11360608,   11195382,   11069140,   10913511,   10807088,   10777181,   
          10913511,   10945905,   11053089,   11242400,   11374666,   11516974,   11514534,   11142743,   10778482,   11030998,   11334394,   11345967,   11162742,   11029246,   10885524,   10781782,   10735810,   
          10889080,   10927678,   11040253,   11211246,   11362415,   11538500,   11523450,   11181813,   10843319,   11081847,   11358853,   11333572,   11156815,   11020302,   10878893,   10795889,   10763078,   
          10891452,   10929206,   11052259,   11180463,   11371407,   11530997,   11547666,   11216124,   10883629,   11116535,   11373424,   11326845,   11173917,   11022050,   10857587,   10793046,   10765441,           
        },
        /*Channel Gb*/
        {   
          10991429,   11029113,   11122111,   11208191,   11355448,   11557383,   11478567,   11203519,   10956403,   11172271,   11511555,   11533598,   11344368,   11198399,   11005270,   10871295,   10826843,   
          11013950,   11049053,   11138665,   11226565,   11390117,   11567706,   11474931,   11171972,   10919118,   11155069,   11520309,   11545692,   11375862,   11203218,   11018028,   10887253,   10849118,   
          11001540,   11050518,   11155667,   11263902,   11434459,   11590557,   11476249,   11135192,   10846716,   11130878,   11537272,   11575849,   11420301,   11250517,   11058243,   10936125,   10888011,   
          11036369,   11091758,   11212666,   11332127,   11473983,   11606703,   11470192,   11088412,   10737083,   11093924,   11536313,   11604063,   11488961,   11314020,   11122606,   11016863,   10981337,   
          11135589,   11162243,   11259234,   11370532,   11536899,   11579817,   11432733,   10966449,   10598409,   10995878,   11479253,   11605841,   11522327,   11376121,   11195440,   11104866,   11073233,   
          11195590,   11228583,   11318887,   11424686,   11562997,   11557383,   11329251,   10800411,   10435750,   10812979,   11445716,   11633226,   11570115,   11414408,   11256901,   11115236,   11061521,   
          11280423,   11304094,   11403628,   11472509,   11584486,   11495830,   11142787,   10590954,   10224697,   10604524,   11315608,   11616196,   11612904,   11525302,   11372653,   11226313,   11174069,   
          11318631,   11347509,   11408521,   11483261,   11554498,   11418945,   10985487,   10380061,   10076677,   10371318,   11119093,   11522964,   11617438,   11536207,   11402536,   11284242,   11240955,   
          11290104,   11311255,   11390169,   11442625,   11428865,   11290716,   10810828,   10271630,   10000000,   10217216,   10962025,   11430067,   11598947,   11512668,   11433518,   11353333,   11332589,   
          11143432,   11151735,   11228785,   11278336,   11360609,   11279659,   10872099,   10334996,   10039764,   10303046,   11007742,   11415086,   11477882,   11413887,   11331510,   11215987,   11179167,   
          11034128,   11060151,   11156563,   11211660,   11346376,   11304707,   11020554,   10525041,   10183250,   10501163,   11161396,   11464142,   11471824,   11352560,   11245759,   11162542,   11104866,   
          10993653,   11005803,   11049492,   11185570,   11330432,   11409458,   11166781,   10675450,   10331066,   10702831,   11274164,   11455001,   11414825,   11235649,   11144078,   11035687,   10983556,   
          10903542,   10937035,   11012834,   11148303,   11313764,   11368153,   11257256,   10867844,   10494506,   10843470,   11363553,   11481415,   11360867,   11231257,   11111234,   10982736,   10943882,   
          10846858,   10898741,   10969915,   11138467,   11308134,   11416285,   11318990,   10994620,   10602185,   10949971,   11412792,   11488539,   11353076,   11183919,   11086002,   10936652,   10880712,   
          10821827,   10860431,   10943834,   11081088,   11281594,   11404149,   11368567,   11046611,   10703427,   11022936,   11418162,   11491919,   11355345,   11158107,   11032618,   10913588,   10867277,   
          10802651,   10828860,   10939524,   11067154,   11261009,   11383892,   11381612,   11052619,   10766914,   11052619,   11424738,   11493082,   11347200,   11156713,   10999991,   10900071,   10878108,   
          10798125,   10834117,   10961063,   11075736,   11248694,   11392817,   11375707,   11064019,   10797332,   11087478,   11429544,   11493822,   11347509,   11136382,   10997280,   10906825,   10895985,
        },
        /*Channel B*/
        {  
          10420989,   10403924,   10418605,   10528790,   10682276,   10777606,   10777606,   10643487,   10547897,   10529196,   10601192,   10635862,   10474211,   10350236,   10236674,   10086993,   10006666,   
          10442979,   10435240,   10439388,   10537319,   10683947,   10783818,   10787395,   10633130,   10531550,   10525382,   10608520,   10637353,   10494329,   10372164,   10240972,   10104155,   10000000,   
          10447532,   10439308,   10451210,   10559231,   10673511,   10804034,   10818924,   10612146,   10486756,   10495055,   10636856,   10639010,   10495620,   10403528,   10267994,   10129107,   10028855,   
          10457371,   10447692,   10488447,   10603908,   10701107,   10826127,   10816953,   10586402,   10427752,   10459212,   10642574,   10645561,   10542280,   10439547,   10311636,   10143022,   10056544,   
          10496507,   10499411,   10539595,   10633047,   10731879,   10839357,   10798567,   10529602,   10391023,   10407174,   10600122,   10657937,   10575501,   10490702,   10366575,   10231384,   10132112,   
          10588536,   10560619,   10570099,   10667760,   10774971,   10842885,   10783307,   10468270,   10329650,   10355493,   10618003,   10707146,   10617178,   10546268,   10428787,   10278503,   10176348,   
          10640087,   10631557,   10658104,   10744627,   10845296,   10849258,   10742683,   10419002,   10311636,   10319427,   10559231,   10721350,   10678602,   10609426,   10507246,   10345611,   10265988,   
          10707566,   10669760,   10718321,   10795750,   10866694,   10896254,   10667760,   10400913,   10253811,   10267454,   10503529,   10707566,   10734072,   10713781,   10592970,   10416936,   10314517,   
          10770213,   10755372,   10789184,   10877165,   10880284,   10880457,   10685034,   10415348,   10287793,   10228625,   10521896,   10750885,   10797884,   10760625,   10688880,   10529764,   10441862,   
          10637933,   10620563,   10645561,   10757405,   10821581,   10825955,   10670676,   10441223,   10308912,   10257584,   10488849,   10695325,   10682276,   10654695,   10554988,   10411775,   10339815,   
          10556701,   10550504,   10568791,   10639010,   10770468,   10784329,   10710085,   10502641,   10352511,   10321064,   10525869,   10695074,   10654695,   10596915,   10488447,   10338485,   10261592,   
          10508054,   10482008,   10482651,   10610415,   10724548,   10824411,   10744373,   10515335,   10386519,   10412727,   10585828,   10646473,   10587305,   10501026,   10386361,   10225103,   10153125,   
          10433646,   10419718,   10430619,   10578040,   10665594,   10820895,   10769109,   10542524,   10415904,   10433487,   10659185,   10626098,   10540409,   10447932,   10342634,   10197088,   10100569,   
          10354316,   10361778,   10413521,   10554010,   10635862,   10784584,   10763254,   10582875,   10441383,   10476862,   10642409,   10588536,   10493362,   10400676,   10279276,   10158486,   10076427,   
          10326604,   10339424,   10374843,   10522788,   10609838,   10789355,   10782626,   10626098,   10476300,   10511127,   10634951,   10603496,   10509510,   10368385,   10287793,   10152672,   10043530,   
          10305645,   10309146,   10362485,   10507812,   10613136,   10758930,   10807196,   10636276,   10508297,   10534068,   10613136,   10611157,   10508054,   10353060,   10292677,   10167107,   10066846,   
          10303857,   10299662,   10376025,   10502641,   10617343,   10752832,   10826556,   10647469,   10535368,   10555722,   10621058,   10609344,   10489816,   10343809,   10279586,   10178092,   10097657,    
        },
    },
	/*Light Source Two*/ 
    {          
        /*AWB RGain*/
        849,
        /*AWB BGain*/
        411,
        /*Channel R */
        {      
          11115904,   11157695,   11260216,   11348563,   11521809,   11640839,   11575170,   11226863,   11057837,   11351025,   11712442,   11746682,   11606037,   11438035,   11289688,   11217863,   11187308,   
          11128393,   11173909,   11274352,   11359328,   11512661,   11624029,   11578790,   11207625,   11018201,   11323735,   11711986,   11758906,   11611300,   11434937,   11268915,   11216556,   11192250,   
          11134055,   11191366,   11258320,   11367483,   11540648,   11645401,   11569606,   11160230,   10940620,   11270445,   11708339,   11779393,   11615054,   11468173,   11284395,   11230059,   11205694,   
          11197718,   11233466,   11285612,   11398060,   11589997,   11666230,   11542641,   11109080,   10835952,   11178423,   11686849,   11815767,   11637068,   11500668,   11329972,   11277363,   11259373,   
          11253740,   11277997,   11360883,   11433851,   11590611,   11619540,   11473749,   10990880,   10689221,   11065921,   11609788,   11791917,   11685942,   11516737,   11405838,   11344658,   11313834,   
          11306234,   11322776,   11384420,   11506056,   11643711,   11641121,   11358578,   10795272,   10481881,   10904832,   11526058,   11756264,   11712442,   11569606,   11470414,   11377747,   11351828,  
          11392557,   11409514,   11453220,   11561215,   11611356,   11550285,   11173494,   10570729,   10256622,   10643841,   11364477,   11711017,   11746395,   11637800,   11565214,   11484480,   11439611,   
          11459764,   11487879,   11537108,   11624422,   11634481,   11495231,   11004398,   10364478,   10094915,   10408313,   11159816,   11622233,   11743759,   11670133,   11602288,   11541312,   11512441,   
          11541368,   11555386,   11606596,   11643711,   11590723,   11401407,   10915712,   10275538,   10000000,   10249546,   11013562,   11545188,   11704581,   11745650,   11634481,   11570051,   11570997,   
          11361419,   11421046,   11522140,   11579458,   11586315,   11461565,   10992436,   10338923,   10052289,   10320303,   11046672,   11523133,   11630882,   11552391,   11523133,   11457691,   11446085,   
          11398923,   11406162,   11470086,   11490127,   11596307,   11526776,   11166340,   10551636,   10199510,   10538285,   11198916,   11553556,   11608780,   11526500,   11431353,   11368879,   11334454,   
          11357881,   11336269,   11368557,   11483001,   11598039,   11663064,   11338138,   10767214,   10367782,   10746541,   11318516,   11550839,   11526334,   11440916,   11327785,   11275408,   11254845,  
          11288047,   11292813,   11342092,   11503362,   11647148,   11695709,   11490730,   10971641,   10549509,   10942013,   11461565,   11629590,   11519879,   11418283,   11309794,   11250847,   11231579,   
          11220164,   11251162,   11298486,   11499130,   11614774,   11706346,   11554554,   11091111,   10675046,   11059158,   11486946,   11609172,   11490456,   11344872,   11225921,   11143386,   11117649,   
          11189441,   11224717,   11295092,   11426740,   11605309,   11681291,   11596866,   11148599,   10781056,   11127878,   11514478,   11597145,   11457691,   11304588,   11201157,   11123507,   11100421,   
          11173494,   11205747,   11295357,   11408487,   11611748,   11650926,   11608332,   11193864,   10853445,   11171730,   11521478,   11597257,   11437165,   11287041,   11181435,   11104877,   11075443,   
          11151337,   11185177,   11296364,   11384204,   11605925,   11654819,   11623749,   11222623,   10888995,   11169294,   11543194,   11579570,   11455673,   11280693,   11162508,   11119447,   11082532,          
            
        },
        /*Channel Gr*/
        {   
          10949500,   10966598,   11014185,   11103847,   11229978,   11370942,   11401743,   11139318,   10983219,   11200096,   11441816,   11424928,   11217634,   11007977,   10900616,   10776113,   10739776,   
          10947151,   10960300,   11020982,   11119798,   11260831,   11404292,   11401483,   11121183,   10963761,   11179913,   11440560,   11440717,   11219597,   11021419,   10887657,   10770218,   10735027,   
          10927678,   10952091,   11026377,   11134109,   11288133,   11411214,   11394417,   11092762,   10901329,   11125884,   11426808,   11459384,   11242299,   11047229,   10921806,   10810827,   10785876,   
          10941403,   10985921,   11081307,   11154824,   11337837,   11407622,   11361021,   11039863,   10804753,   11037475,   11420284,   11480801,   11293130,   11099706,   10960445,   10855984,   10815082,   
          11022634,   11042545,   11109274,   11219698,   11347717,   11406425,   11299356,   10923524,   10632989,   10902898,   11363293,   11467636,   11318231,   11194730,   11015932,   10900949,   10832936,   
          11095273,   11114805,   11168726,   11247053,   11357718,   11405228,   11198340,   10718505,   10436848,   10769707,   11288286,   11438466,   11361176,   11221208,   11097785,   10973048,   10916370,   
          11179363,   11197438,   11242653,   11320178,   11392444,   11325204,   11047082,   10550386,   10212148,   10524007,   11151789,   11397066,   11398832,   11314441,   11166182,   11056413,   11011808,   
          11228717,   11252013,   11319153,   11370942,   11404812,   11314851,   10908273,   10349620,   10060073,   10304916,   10965588,   11352200,   11453242,   11339894,   11238257,   11114311,   11051135,   
          11298590,   11315107,   11371717,   11427383,   11415330,   11286044,   10859614,   10304703,   10000000,   10173421,   10865939,   11294405,   11449466,   11404656,   11306715,   11206120,   11157761,   
          11216577,   11248723,   11301757,   11352973,   11429890,   11316541,   10951563,   10387459,   10062421,   10252475,   10919850,   11291141,   11358492,   11286706,   11176165,   11095618,   11061990,   
          11157114,   11176515,   11221208,   11296651,   11426443,   11393378,   11124845,   10562286,   10185523,   10445437,   11037475,   11311011,   11307379,   11181413,   11135002,   11031047,   10985101,   
          11062332,   11081602,   11154475,   11266261,   11410537,   11508015,   11261085,   10757243,   10345980,   10664920,   11163390,   11320999,   11272407,   11148855,   11029344,   10930353,   10885809,   
          11001535,   11013990,   11094141,   11248875,   11427905,   11525681,   11418823,   10949500,   10560323,   10830495,   11258752,   11375339,   11246547,   11117622,   10985680,   10886235,   10870190,   
          10961261,   10969822,   11051477,   11241996,   11400963,   11536850,   11468162,   11053578,   10669562,   10927296,   11314031,   11360608,   11195382,   11069140,   10913511,   10807088,   10777181,   
          10913511,   10945905,   11053089,   11242400,   11374666,   11516974,   11514534,   11142743,   10778482,   11030998,   11334394,   11345967,   11162742,   11029246,   10885524,   10781782,   10735810,   
          10889080,   10927678,   11040253,   11211246,   11362415,   11538500,   11523450,   11181813,   10843319,   11081847,   11358853,   11333572,   11156815,   11020302,   10878893,   10795889,   10763078,   
          10891452,   10929206,   11052259,   11180463,   11371407,   11530997,   11547666,   11216124,   10883629,   11116535,   11373424,   11326845,   11173917,   11022050,   10857587,   10793046,   10765441,           
        },  
        /*Channel Gb*/
        {   
          10991429,   11029113,   11122111,   11208191,   11355448,   11557383,   11478567,   11203519,   10956403,   11172271,   11511555,   11533598,   11344368,   11198399,   11005270,   10871295,   10826843,   
          11013950,   11049053,   11138665,   11226565,   11390117,   11567706,   11474931,   11171972,   10919118,   11155069,   11520309,   11545692,   11375862,   11203218,   11018028,   10887253,   10849118,   
          11001540,   11050518,   11155667,   11263902,   11434459,   11590557,   11476249,   11135192,   10846716,   11130878,   11537272,   11575849,   11420301,   11250517,   11058243,   10936125,   10888011,   
          11036369,   11091758,   11212666,   11332127,   11473983,   11606703,   11470192,   11088412,   10737083,   11093924,   11536313,   11604063,   11488961,   11314020,   11122606,   11016863,   10981337,   
          11135589,   11162243,   11259234,   11370532,   11536899,   11579817,   11432733,   10966449,   10598409,   10995878,   11479253,   11605841,   11522327,   11376121,   11195440,   11104866,   11073233,   
          11195590,   11228583,   11318887,   11424686,   11562997,   11557383,   11329251,   10800411,   10435750,   10812979,   11445716,   11633226,   11570115,   11414408,   11256901,   11115236,   11061521,   
          11280423,   11304094,   11403628,   11472509,   11584486,   11495830,   11142787,   10590954,   10224697,   10604524,   11315608,   11616196,   11612904,   11525302,   11372653,   11226313,   11174069,   
          11318631,   11347509,   11408521,   11483261,   11554498,   11418945,   10985487,   10380061,   10076677,   10371318,   11119093,   11522964,   11617438,   11536207,   11402536,   11284242,   11240955,   
          11290104,   11311255,   11390169,   11442625,   11428865,   11290716,   10810828,   10271630,   10000000,   10217216,   10962025,   11430067,   11598947,   11512668,   11433518,   11353333,   11332589,   
          11143432,   11151735,   11228785,   11278336,   11360609,   11279659,   10872099,   10334996,   10039764,   10303046,   11007742,   11415086,   11477882,   11413887,   11331510,   11215987,   11179167,   
          11034128,   11060151,   11156563,   11211660,   11346376,   11304707,   11020554,   10525041,   10183250,   10501163,   11161396,   11464142,   11471824,   11352560,   11245759,   11162542,   11104866,   
          10993653,   11005803,   11049492,   11185570,   11330432,   11409458,   11166781,   10675450,   10331066,   10702831,   11274164,   11455001,   11414825,   11235649,   11144078,   11035687,   10983556,   
          10903542,   10937035,   11012834,   11148303,   11313764,   11368153,   11257256,   10867844,   10494506,   10843470,   11363553,   11481415,   11360867,   11231257,   11111234,   10982736,   10943882,   
          10846858,   10898741,   10969915,   11138467,   11308134,   11416285,   11318990,   10994620,   10602185,   10949971,   11412792,   11488539,   11353076,   11183919,   11086002,   10936652,   10880712,   
          10821827,   10860431,   10943834,   11081088,   11281594,   11404149,   11368567,   11046611,   10703427,   11022936,   11418162,   11491919,   11355345,   11158107,   11032618,   10913588,   10867277,   
          10802651,   10828860,   10939524,   11067154,   11261009,   11383892,   11381612,   11052619,   10766914,   11052619,   11424738,   11493082,   11347200,   11156713,   10999991,   10900071,   10878108,   
          10798125,   10834117,   10961063,   11075736,   11248694,   11392817,   11375707,   11064019,   10797332,   11087478,   11429544,   11493822,   11347509,   11136382,   10997280,   10906825,   10895985,
        }, 
        /*Channel B*/
        {   
              
          10420989,   10403924,   10418605,   10528790,   10682276,   10777606,   10777606,   10643487,   10547897,   10529196,   10601192,   10635862,   10474211,   10350236,   10236674,   10086993,   10006666,   
          10442979,   10435240,   10439388,   10537319,   10683947,   10783818,   10787395,   10633130,   10531550,   10525382,   10608520,   10637353,   10494329,   10372164,   10240972,   10104155,   10000000,   
          10447532,   10439308,   10451210,   10559231,   10673511,   10804034,   10818924,   10612146,   10486756,   10495055,   10636856,   10639010,   10495620,   10403528,   10267994,   10129107,   10028855,   
          10457371,   10447692,   10488447,   10603908,   10701107,   10826127,   10816953,   10586402,   10427752,   10459212,   10642574,   10645561,   10542280,   10439547,   10311636,   10143022,   10056544,   
          10496507,   10499411,   10539595,   10633047,   10731879,   10839357,   10798567,   10529602,   10391023,   10407174,   10600122,   10657937,   10575501,   10490702,   10366575,   10231384,   10132112,   
          10588536,   10560619,   10570099,   10667760,   10774971,   10842885,   10783307,   10468270,   10329650,   10355493,   10618003,   10707146,   10617178,   10546268,   10428787,   10278503,   10176348,   
          10640087,   10631557,   10658104,   10744627,   10845296,   10849258,   10742683,   10419002,   10311636,   10319427,   10559231,   10721350,   10678602,   10609426,   10507246,   10345611,   10265988,   
          10707566,   10669760,   10718321,   10795750,   10866694,   10896254,   10667760,   10400913,   10253811,   10267454,   10503529,   10707566,   10734072,   10713781,   10592970,   10416936,   10314517,   
          10770213,   10755372,   10789184,   10877165,   10880284,   10880457,   10685034,   10415348,   10287793,   10228625,   10521896,   10750885,   10797884,   10760625,   10688880,   10529764,   10441862,   
          10637933,   10620563,   10645561,   10757405,   10821581,   10825955,   10670676,   10441223,   10308912,   10257584,   10488849,   10695325,   10682276,   10654695,   10554988,   10411775,   10339815,   
          10556701,   10550504,   10568791,   10639010,   10770468,   10784329,   10710085,   10502641,   10352511,   10321064,   10525869,   10695074,   10654695,   10596915,   10488447,   10338485,   10261592,   
          10508054,   10482008,   10482651,   10610415,   10724548,   10824411,   10744373,   10515335,   10386519,   10412727,   10585828,   10646473,   10587305,   10501026,   10386361,   10225103,   10153125,   
          10433646,   10419718,   10430619,   10578040,   10665594,   10820895,   10769109,   10542524,   10415904,   10433487,   10659185,   10626098,   10540409,   10447932,   10342634,   10197088,   10100569,   
          10354316,   10361778,   10413521,   10554010,   10635862,   10784584,   10763254,   10582875,   10441383,   10476862,   10642409,   10588536,   10493362,   10400676,   10279276,   10158486,   10076427,   
          10326604,   10339424,   10374843,   10522788,   10609838,   10789355,   10782626,   10626098,   10476300,   10511127,   10634951,   10603496,   10509510,   10368385,   10287793,   10152672,   10043530,   
          10305645,   10309146,   10362485,   10507812,   10613136,   10758930,   10807196,   10636276,   10508297,   10534068,   10613136,   10611157,   10508054,   10353060,   10292677,   10167107,   10066846,   
          10303857,   10299662,   10376025,   10502641,   10617343,   10752832,   10826556,   10647469,   10535368,   10555722,   10621058,   10609344,   10489816,   10343809,   10279586,   10178092,   10097657,
               
        },

    },
	/*Light Source Three*/
    {        
        /*AWB RGain*/
        662,
        /*AWB BGain*/
        526,
        /*Channel R*/
        {    
          11115904,   11157695,   11260216,   11348563,   11521809,   11640839,   11575170,   11226863,   11057837,   11351025,   11712442,   11746682,   11606037,   11438035,   11289688,   11217863,   11187308,   
          11128393,   11173909,   11274352,   11359328,   11512661,   11624029,   11578790,   11207625,   11018201,   11323735,   11711986,   11758906,   11611300,   11434937,   11268915,   11216556,   11192250,   
          11134055,   11191366,   11258320,   11367483,   11540648,   11645401,   11569606,   11160230,   10940620,   11270445,   11708339,   11779393,   11615054,   11468173,   11284395,   11230059,   11205694,   
          11197718,   11233466,   11285612,   11398060,   11589997,   11666230,   11542641,   11109080,   10835952,   11178423,   11686849,   11815767,   11637068,   11500668,   11329972,   11277363,   11259373,   
          11253740,   11277997,   11360883,   11433851,   11590611,   11619540,   11473749,   10990880,   10689221,   11065921,   11609788,   11791917,   11685942,   11516737,   11405838,   11344658,   11313834,   
          11306234,   11322776,   11384420,   11506056,   11643711,   11641121,   11358578,   10795272,   10481881,   10904832,   11526058,   11756264,   11712442,   11569606,   11470414,   11377747,   11351828,  
          11392557,   11409514,   11453220,   11561215,   11611356,   11550285,   11173494,   10570729,   10256622,   10643841,   11364477,   11711017,   11746395,   11637800,   11565214,   11484480,   11439611,   
          11459764,   11487879,   11537108,   11624422,   11634481,   11495231,   11004398,   10364478,   10094915,   10408313,   11159816,   11622233,   11743759,   11670133,   11602288,   11541312,   11512441,   
          11541368,   11555386,   11606596,   11643711,   11590723,   11401407,   10915712,   10275538,   10000000,   10249546,   11013562,   11545188,   11704581,   11745650,   11634481,   11570051,   11570997,   
          11361419,   11421046,   11522140,   11579458,   11586315,   11461565,   10992436,   10338923,   10052289,   10320303,   11046672,   11523133,   11630882,   11552391,   11523133,   11457691,   11446085,   
          11398923,   11406162,   11470086,   11490127,   11596307,   11526776,   11166340,   10551636,   10199510,   10538285,   11198916,   11553556,   11608780,   11526500,   11431353,   11368879,   11334454,   
          11357881,   11336269,   11368557,   11483001,   11598039,   11663064,   11338138,   10767214,   10367782,   10746541,   11318516,   11550839,   11526334,   11440916,   11327785,   11275408,   11254845,  
          11288047,   11292813,   11342092,   11503362,   11647148,   11695709,   11490730,   10971641,   10549509,   10942013,   11461565,   11629590,   11519879,   11418283,   11309794,   11250847,   11231579,   
          11220164,   11251162,   11298486,   11499130,   11614774,   11706346,   11554554,   11091111,   10675046,   11059158,   11486946,   11609172,   11490456,   11344872,   11225921,   11143386,   11117649,   
          11189441,   11224717,   11295092,   11426740,   11605309,   11681291,   11596866,   11148599,   10781056,   11127878,   11514478,   11597145,   11457691,   11304588,   11201157,   11123507,   11100421,   
          11173494,   11205747,   11295357,   11408487,   11611748,   11650926,   11608332,   11193864,   10853445,   11171730,   11521478,   11597257,   11437165,   11287041,   11181435,   11104877,   11075443,   
          11151337,   11185177,   11296364,   11384204,   11605925,   11654819,   11623749,   11222623,   10888995,   11169294,   11543194,   11579570,   11455673,   11280693,   11162508,   11119447,   11082532,          
             
        },
        /*Channel Gr*/
        {  
          10949500,   10966598,   11014185,   11103847,   11229978,   11370942,   11401743,   11139318,   10983219,   11200096,   11441816,   11424928,   11217634,   11007977,   10900616,   10776113,   10739776,   
          10947151,   10960300,   11020982,   11119798,   11260831,   11404292,   11401483,   11121183,   10963761,   11179913,   11440560,   11440717,   11219597,   11021419,   10887657,   10770218,   10735027,   
          10927678,   10952091,   11026377,   11134109,   11288133,   11411214,   11394417,   11092762,   10901329,   11125884,   11426808,   11459384,   11242299,   11047229,   10921806,   10810827,   10785876,   
          10941403,   10985921,   11081307,   11154824,   11337837,   11407622,   11361021,   11039863,   10804753,   11037475,   11420284,   11480801,   11293130,   11099706,   10960445,   10855984,   10815082,   
          11022634,   11042545,   11109274,   11219698,   11347717,   11406425,   11299356,   10923524,   10632989,   10902898,   11363293,   11467636,   11318231,   11194730,   11015932,   10900949,   10832936,   
          11095273,   11114805,   11168726,   11247053,   11357718,   11405228,   11198340,   10718505,   10436848,   10769707,   11288286,   11438466,   11361176,   11221208,   11097785,   10973048,   10916370,   
          11179363,   11197438,   11242653,   11320178,   11392444,   11325204,   11047082,   10550386,   10212148,   10524007,   11151789,   11397066,   11398832,   11314441,   11166182,   11056413,   11011808,   
          11228717,   11252013,   11319153,   11370942,   11404812,   11314851,   10908273,   10349620,   10060073,   10304916,   10965588,   11352200,   11453242,   11339894,   11238257,   11114311,   11051135,   
          11298590,   11315107,   11371717,   11427383,   11415330,   11286044,   10859614,   10304703,   10000000,   10173421,   10865939,   11294405,   11449466,   11404656,   11306715,   11206120,   11157761,   
          11216577,   11248723,   11301757,   11352973,   11429890,   11316541,   10951563,   10387459,   10062421,   10252475,   10919850,   11291141,   11358492,   11286706,   11176165,   11095618,   11061990,   
          11157114,   11176515,   11221208,   11296651,   11426443,   11393378,   11124845,   10562286,   10185523,   10445437,   11037475,   11311011,   11307379,   11181413,   11135002,   11031047,   10985101,   
          11062332,   11081602,   11154475,   11266261,   11410537,   11508015,   11261085,   10757243,   10345980,   10664920,   11163390,   11320999,   11272407,   11148855,   11029344,   10930353,   10885809,   
          11001535,   11013990,   11094141,   11248875,   11427905,   11525681,   11418823,   10949500,   10560323,   10830495,   11258752,   11375339,   11246547,   11117622,   10985680,   10886235,   10870190,   
          10961261,   10969822,   11051477,   11241996,   11400963,   11536850,   11468162,   11053578,   10669562,   10927296,   11314031,   11360608,   11195382,   11069140,   10913511,   10807088,   10777181,   
          10913511,   10945905,   11053089,   11242400,   11374666,   11516974,   11514534,   11142743,   10778482,   11030998,   11334394,   11345967,   11162742,   11029246,   10885524,   10781782,   10735810,   
          10889080,   10927678,   11040253,   11211246,   11362415,   11538500,   11523450,   11181813,   10843319,   11081847,   11358853,   11333572,   11156815,   11020302,   10878893,   10795889,   10763078,   
          10891452,   10929206,   11052259,   11180463,   11371407,   11530997,   11547666,   11216124,   10883629,   11116535,   11373424,   11326845,   11173917,   11022050,   10857587,   10793046,   10765441,           
        },
        /*Channel Gb*/
        {   
          10991429,   11029113,   11122111,   11208191,   11355448,   11557383,   11478567,   11203519,   10956403,   11172271,   11511555,   11533598,   11344368,   11198399,   11005270,   10871295,   10826843,   
          11013950,   11049053,   11138665,   11226565,   11390117,   11567706,   11474931,   11171972,   10919118,   11155069,   11520309,   11545692,   11375862,   11203218,   11018028,   10887253,   10849118,   
          11001540,   11050518,   11155667,   11263902,   11434459,   11590557,   11476249,   11135192,   10846716,   11130878,   11537272,   11575849,   11420301,   11250517,   11058243,   10936125,   10888011,   
          11036369,   11091758,   11212666,   11332127,   11473983,   11606703,   11470192,   11088412,   10737083,   11093924,   11536313,   11604063,   11488961,   11314020,   11122606,   11016863,   10981337,   
          11135589,   11162243,   11259234,   11370532,   11536899,   11579817,   11432733,   10966449,   10598409,   10995878,   11479253,   11605841,   11522327,   11376121,   11195440,   11104866,   11073233,   
          11195590,   11228583,   11318887,   11424686,   11562997,   11557383,   11329251,   10800411,   10435750,   10812979,   11445716,   11633226,   11570115,   11414408,   11256901,   11115236,   11061521,   
          11280423,   11304094,   11403628,   11472509,   11584486,   11495830,   11142787,   10590954,   10224697,   10604524,   11315608,   11616196,   11612904,   11525302,   11372653,   11226313,   11174069,   
          11318631,   11347509,   11408521,   11483261,   11554498,   11418945,   10985487,   10380061,   10076677,   10371318,   11119093,   11522964,   11617438,   11536207,   11402536,   11284242,   11240955,   
          11290104,   11311255,   11390169,   11442625,   11428865,   11290716,   10810828,   10271630,   10000000,   10217216,   10962025,   11430067,   11598947,   11512668,   11433518,   11353333,   11332589,   
          11143432,   11151735,   11228785,   11278336,   11360609,   11279659,   10872099,   10334996,   10039764,   10303046,   11007742,   11415086,   11477882,   11413887,   11331510,   11215987,   11179167,   
          11034128,   11060151,   11156563,   11211660,   11346376,   11304707,   11020554,   10525041,   10183250,   10501163,   11161396,   11464142,   11471824,   11352560,   11245759,   11162542,   11104866,   
          10993653,   11005803,   11049492,   11185570,   11330432,   11409458,   11166781,   10675450,   10331066,   10702831,   11274164,   11455001,   11414825,   11235649,   11144078,   11035687,   10983556,   
          10903542,   10937035,   11012834,   11148303,   11313764,   11368153,   11257256,   10867844,   10494506,   10843470,   11363553,   11481415,   11360867,   11231257,   11111234,   10982736,   10943882,   
          10846858,   10898741,   10969915,   11138467,   11308134,   11416285,   11318990,   10994620,   10602185,   10949971,   11412792,   11488539,   11353076,   11183919,   11086002,   10936652,   10880712,   
          10821827,   10860431,   10943834,   11081088,   11281594,   11404149,   11368567,   11046611,   10703427,   11022936,   11418162,   11491919,   11355345,   11158107,   11032618,   10913588,   10867277,   
          10802651,   10828860,   10939524,   11067154,   11261009,   11383892,   11381612,   11052619,   10766914,   11052619,   11424738,   11493082,   11347200,   11156713,   10999991,   10900071,   10878108,   
          10798125,   10834117,   10961063,   11075736,   11248694,   11392817,   11375707,   11064019,   10797332,   11087478,   11429544,   11493822,   11347509,   11136382,   10997280,   10906825,   10895985,
        }, 
        /*Channel B*/
        {  
          
          10420989,   10403924,   10418605,   10528790,   10682276,   10777606,   10777606,   10643487,   10547897,   10529196,   10601192,   10635862,   10474211,   10350236,   10236674,   10086993,   10006666,   
          10442979,   10435240,   10439388,   10537319,   10683947,   10783818,   10787395,   10633130,   10531550,   10525382,   10608520,   10637353,   10494329,   10372164,   10240972,   10104155,   10000000,   
          10447532,   10439308,   10451210,   10559231,   10673511,   10804034,   10818924,   10612146,   10486756,   10495055,   10636856,   10639010,   10495620,   10403528,   10267994,   10129107,   10028855,   
          10457371,   10447692,   10488447,   10603908,   10701107,   10826127,   10816953,   10586402,   10427752,   10459212,   10642574,   10645561,   10542280,   10439547,   10311636,   10143022,   10056544,   
          10496507,   10499411,   10539595,   10633047,   10731879,   10839357,   10798567,   10529602,   10391023,   10407174,   10600122,   10657937,   10575501,   10490702,   10366575,   10231384,   10132112,   
          10588536,   10560619,   10570099,   10667760,   10774971,   10842885,   10783307,   10468270,   10329650,   10355493,   10618003,   10707146,   10617178,   10546268,   10428787,   10278503,   10176348,   
          10640087,   10631557,   10658104,   10744627,   10845296,   10849258,   10742683,   10419002,   10311636,   10319427,   10559231,   10721350,   10678602,   10609426,   10507246,   10345611,   10265988,   
          10707566,   10669760,   10718321,   10795750,   10866694,   10896254,   10667760,   10400913,   10253811,   10267454,   10503529,   10707566,   10734072,   10713781,   10592970,   10416936,   10314517,   
          10770213,   10755372,   10789184,   10877165,   10880284,   10880457,   10685034,   10415348,   10287793,   10228625,   10521896,   10750885,   10797884,   10760625,   10688880,   10529764,   10441862,   
          10637933,   10620563,   10645561,   10757405,   10821581,   10825955,   10670676,   10441223,   10308912,   10257584,   10488849,   10695325,   10682276,   10654695,   10554988,   10411775,   10339815,   
          10556701,   10550504,   10568791,   10639010,   10770468,   10784329,   10710085,   10502641,   10352511,   10321064,   10525869,   10695074,   10654695,   10596915,   10488447,   10338485,   10261592,   
          10508054,   10482008,   10482651,   10610415,   10724548,   10824411,   10744373,   10515335,   10386519,   10412727,   10585828,   10646473,   10587305,   10501026,   10386361,   10225103,   10153125,   
          10433646,   10419718,   10430619,   10578040,   10665594,   10820895,   10769109,   10542524,   10415904,   10433487,   10659185,   10626098,   10540409,   10447932,   10342634,   10197088,   10100569,   
          10354316,   10361778,   10413521,   10554010,   10635862,   10784584,   10763254,   10582875,   10441383,   10476862,   10642409,   10588536,   10493362,   10400676,   10279276,   10158486,   10076427,   
          10326604,   10339424,   10374843,   10522788,   10609838,   10789355,   10782626,   10626098,   10476300,   10511127,   10634951,   10603496,   10509510,   10368385,   10287793,   10152672,   10043530,   
          10305645,   10309146,   10362485,   10507812,   10613136,   10758930,   10807196,   10636276,   10508297,   10534068,   10613136,   10611157,   10508054,   10353060,   10292677,   10167107,   10066846,   
          10303857,   10299662,   10376025,   10502641,   10617343,   10752832,   10826556,   10647469,   10535368,   10555722,   10621058,   10609344,   10489816,   10343809,   10279586,   10178092,   10097657,
        },
    }
};

static ISP_CMOS_DRC_S g_stIspDrc =
{
    0,
    10,
    0,
    2,
    192,
    60,
    0,
    0,
    0,
    {1024,1024,1024,1024,1024,1024,1024,1024,1024,1024,1024,1024,1024,1024,1024,1024,1024,1024,1024,1024,1024,1024,1024,1024,1024,1024,1024,1024,1024,1024,1024,1024,1024}
};

HI_U32 cmos_get_isp_default(ISP_CMOS_DEFAULT_S *pstDef)
{
    if (HI_NULL == pstDef)
    {
        printf("null pointer when get isp default value!\n");
        return -1;
    }

    memset(pstDef, 0, sizeof(ISP_CMOS_DEFAULT_S));

    memcpy(&pstDef->stLsc.stLscUniParaTable, &g_stCmosLscUniTable, sizeof(ISP_LSC_CABLI_UNI_TABLE_S));
    memcpy(&pstDef->stLsc.stLscParaTable[0], &g_stCmosLscTable[0], sizeof(ISP_LSC_CABLI_TABLE_S)*HI_ISP_LSC_LIGHT_NUM);  
    memcpy(&pstDef->stDrc, &g_stIspDrc, sizeof(ISP_CMOS_DRC_S));
    memcpy(&pstDef->stDemosaic, &g_stIspDemosaic, sizeof(ISP_CMOS_DEMOSAIC_S));
    memcpy(&pstDef->stRgbSharpen, &g_stIspRgbSharpen, sizeof(ISP_CMOS_RGBSHARPEN_S));
    memcpy(&pstDef->stGe, &g_stIspGe, sizeof(ISP_CMOS_GE_S));			

    pstDef->stNoiseTbl.stNrCaliPara.u8CalicoefRow = DMNR_CALIB_CARVE_NUM_OV9712;
    pstDef->stNoiseTbl.stNrCaliPara.pCalibcoef    = (HI_FLOAT (*)[4])g_coef_calib_ov9712;

    memcpy(&pstDef->stNoiseTbl.stIsoParaTable[0], &g_stNrIsoParaTab[0],sizeof(ISP_NR_ISO_PARA_TABLE_S)*HI_ISP_NR_ISO_LEVEL_MAX);

    memcpy(&pstDef->stUvnr,       &g_stIspUVNR,       sizeof(ISP_CMOS_UVNR_S));
    memcpy(&pstDef->stDpc,       &g_stCmosDpc,       sizeof(ISP_CMOS_DPC_S));


    pstDef->stSensorMaxResolution.u32MaxWidth  = 1280;
    pstDef->stSensorMaxResolution.u32MaxHeight = 720;

    return 0;
}

HI_U32 cmos_get_isp_black_level(ISP_CMOS_BLACK_LEVEL_S *pstBlackLevel)
{
    
    if (HI_NULL == pstBlackLevel)
    {
        printf("null pointer when get isp black level value!\n");
        return -1;
    }

    /* Don't need to update black level when iso change */
    pstBlackLevel->bUpdate = HI_FALSE;
          
    pstBlackLevel->au16BlackLevel[0] = 0x46;
    pstBlackLevel->au16BlackLevel[1] = 0x45;
    pstBlackLevel->au16BlackLevel[2] = 0x46;
    pstBlackLevel->au16BlackLevel[3] = 0x46;
    

    return 0;  
    
}

HI_VOID cmos_set_pixel_detect(HI_BOOL bEnable)
{

    if (bEnable) /* setup for ISP pixel calibration mode */
    {
        /* 5 fps */
        sensor_write_register(0x2d, 0xd2); 
        sensor_write_register(0x2e, 0x0f); 
        
        /* min gain */
        sensor_write_register(0x0, 0x00);

        /* max exposure time*/
        sensor_write_register(0x10, 0xf8);
        sensor_write_register(0x16, 0x12);
    }
    else /* setup for ISP 'normal mode' */
    {
        sensor_write_register(0x2d, 0x0);
        sensor_write_register(0x2e, 0x0);
        
        bInit = HI_FALSE;
    }

    return;
}

HI_VOID cmos_set_wdr_mode(HI_U8 u8Mode)
{
    bInit = HI_FALSE;
    
    switch(u8Mode)
    {
        case WDR_MODE_NONE:
            if (SENSOR_720P_30FPS_MODE == gu8SensorImageMode)
            {
                gu32FullLinesStd = VMAX_720P30_LINEAR;
            }
            genSensorMode = WDR_MODE_NONE;
            printf("linear mode\n");
        break;
        default:
            printf("NOT support this mode!\n");
            return;
        break;
    }
    
    return;
}

HI_U32 cmos_get_sns_regs_info(ISP_SNS_REGS_INFO_S *pstSnsRegsInfo)
{
    HI_S32 i;

    if (HI_FALSE == bInit)
    {
        g_stSnsRegsInfo.enSnsType = ISP_SNS_I2C_TYPE;
        g_stSnsRegsInfo.u8Cfg2ValidDelayMax = 2;		
        g_stSnsRegsInfo.u32RegNum = 5;

#if(CMOS_OV9712_SLOW_FRAMERATE_MODE == 0)
        g_stSnsRegsInfo.u32RegNum = 7;
#endif

        for (i=0; i<g_stSnsRegsInfo.u32RegNum; i++)
        {	
            g_stSnsRegsInfo.astI2cData[i].bUpdate = HI_TRUE;
            g_stSnsRegsInfo.astI2cData[i].u8DevAddr = sensor_i2c_addr;
            g_stSnsRegsInfo.astI2cData[i].u32AddrByteNum = sensor_addr_byte;
            g_stSnsRegsInfo.astI2cData[i].u32DataByteNum = sensor_data_byte;
        }		
        g_stSnsRegsInfo.astI2cData[0].u8DelayFrmNum = 0;
        g_stSnsRegsInfo.astI2cData[0].u32RegAddr = 0x10;  //ae_ctrl_msb
        g_stSnsRegsInfo.astI2cData[1].u8DelayFrmNum = 0;
        g_stSnsRegsInfo.astI2cData[1].u32RegAddr = 0x16;  //ae_ctrl_lsb
        g_stSnsRegsInfo.astI2cData[2].u8DelayFrmNum = 1;
        g_stSnsRegsInfo.astI2cData[2].u32RegAddr = 0x00;  //dgain  

        g_stSnsRegsInfo.astI2cData[3].u8DelayFrmNum = 1;
        g_stSnsRegsInfo.astI2cData[3].u32RegAddr = 0x2a;
        g_stSnsRegsInfo.astI2cData[4].u8DelayFrmNum = 1;
        g_stSnsRegsInfo.astI2cData[4].u32RegAddr = 0x2b; 
        
#if(CMOS_OV9712_SLOW_FRAMERATE_MODE == 0)
        g_stSnsRegsInfo.astI2cData[5].u8DelayFrmNum = 0;
        g_stSnsRegsInfo.astI2cData[5].u32RegAddr = 0x2d;
        g_stSnsRegsInfo.astI2cData[6].u8DelayFrmNum = 0;
        g_stSnsRegsInfo.astI2cData[6].u32RegAddr = 0x2e;
#endif  
        bInit = HI_TRUE;
    }
    else    
    {        
        for (i=0; i<g_stSnsRegsInfo.u32RegNum; i++)        
        {            
            if (g_stSnsRegsInfo.astI2cData[i].u32Data == g_stPreSnsRegsInfo.astI2cData[i].u32Data)            
            {                
                g_stSnsRegsInfo.astI2cData[i].bUpdate = HI_FALSE;
            }            
            else            
            {                
                g_stSnsRegsInfo.astI2cData[i].bUpdate = HI_TRUE;
            }        
        }    
    }

    if (HI_NULL == pstSnsRegsInfo)
    {
        printf("null pointer when get sns reg info!\n");
        return -1;
    }

    memcpy(pstSnsRegsInfo, &g_stSnsRegsInfo, sizeof(ISP_SNS_REGS_INFO_S)); 
    memcpy(&g_stPreSnsRegsInfo, &g_stSnsRegsInfo, sizeof(ISP_SNS_REGS_INFO_S)); 

    return 0;
}

static HI_S32 cmos_set_image_mode(ISP_CMOS_SENSOR_IMAGE_MODE_S *pstSensorImageMode)
{
    HI_U8 u8SensorImageMode = gu8SensorImageMode;

    bInit = HI_FALSE;
    
    if (HI_NULL == pstSensorImageMode )
    {
        printf("null pointer when set image mode\n");
        return -1;
    }

    if ((pstSensorImageMode->u16Width <= 1280) && (pstSensorImageMode->u16Height <= 720))
    {
        if (WDR_MODE_NONE == genSensorMode)
        {
            if (pstSensorImageMode->f32Fps <= 30)
            {
                u8SensorImageMode = SENSOR_720P_30FPS_MODE;
            }
            else
            {
                printf("Not support! Width:%d, Height:%d, Fps:%f, WDRMode:%d\n", 
                    pstSensorImageMode->u16Width, 
                    pstSensorImageMode->u16Height,
                    pstSensorImageMode->f32Fps,
                    genSensorMode);

                return -1;
            }
        }
        else
        {
            printf("Not support! Width:%d, Height:%d, Fps:%f, WDRMode:%d\n", 
                pstSensorImageMode->u16Width, 
                pstSensorImageMode->u16Height,
                pstSensorImageMode->f32Fps,
                genSensorMode);

            return -1;
        }
    }
    else
    {
        printf("Not support! Width:%d, Height:%d, Fps:%f, WDRMode:%d\n", 
            pstSensorImageMode->u16Width, 
            pstSensorImageMode->u16Height,
            pstSensorImageMode->f32Fps,
            genSensorMode);

        return -1;
    }

    /* Sensor first init */
    if (HI_FALSE == bSensorInit)
    {
        gu8SensorImageMode = u8SensorImageMode;
        
        return 0;
    }

    /* Switch SensorImageMode */
    if (u8SensorImageMode == gu8SensorImageMode)
    {
        /* Don't need to switch SensorImageMode */
        return -1;
    }
    
    gu8SensorImageMode = u8SensorImageMode;

    return 0;
}



int  sensor_set_inifile_path(const char *pcPath)
{
    memset(pcName, 0, sizeof(pcName));
        
    if (HI_NULL == pcPath)
    {        
        strncat(pcName, "configs/", strlen("configs/"));
        strncat(pcName, CMOS_CFG_INI, sizeof(CMOS_CFG_INI));
    }
    else
    {
		if(strlen(pcPath) > (PATHLEN_MAX - 30))
		{
			printf("Set inifile path is larger PATHLEN_MAX!\n");
			return -1;        
		}
        strncat(pcName, pcPath, strlen(pcPath));
        strncat(pcName, CMOS_CFG_INI, sizeof(CMOS_CFG_INI));
    }
    
    return 0;

}

HI_VOID sensor_global_init()
{   
    gu8SensorImageMode = SENSOR_720P_30FPS_MODE;
    genSensorMode = WDR_MODE_NONE;
    gu32FullLinesStd = VMAX_720P30_LINEAR; 
    gu32FullLines = VMAX_720P30_LINEAR;
    bInit = HI_FALSE;
    bSensorInit = HI_FALSE; 

    memset(&g_stSnsRegsInfo, 0, sizeof(ISP_SNS_REGS_INFO_S));
    memset(&g_stPreSnsRegsInfo, 0, sizeof(ISP_SNS_REGS_INFO_S));
}

HI_S32 cmos_init_sensor_exp_function(ISP_SENSOR_EXP_FUNC_S *pstSensorExpFunc)
{
    memset(pstSensorExpFunc, 0, sizeof(ISP_SENSOR_EXP_FUNC_S));

    pstSensorExpFunc->pfn_cmos_sensor_init = sensor_init;
    pstSensorExpFunc->pfn_cmos_sensor_exit = sensor_exit;
    pstSensorExpFunc->pfn_cmos_sensor_global_init = sensor_global_init;
    pstSensorExpFunc->pfn_cmos_set_image_mode = cmos_set_image_mode;
    pstSensorExpFunc->pfn_cmos_set_wdr_mode = cmos_set_wdr_mode;
    
    pstSensorExpFunc->pfn_cmos_get_isp_default = cmos_get_isp_default;
    pstSensorExpFunc->pfn_cmos_get_isp_black_level = cmos_get_isp_black_level;
    pstSensorExpFunc->pfn_cmos_set_pixel_detect = cmos_set_pixel_detect;
    pstSensorExpFunc->pfn_cmos_get_sns_reg_info = cmos_get_sns_regs_info;

    return 0;
}

/****************************************************************************
 * callback structure                                                       *
 ****************************************************************************/
 
int sensor_register_callback(void)
{
    ISP_DEV IspDev = 0;
    HI_S32 s32Ret;
    ALG_LIB_S stLib;
    ISP_SENSOR_REGISTER_S stIspRegister;
    AE_SENSOR_REGISTER_S  stAeRegister;
    AWB_SENSOR_REGISTER_S stAwbRegister;

    cmos_init_sensor_exp_function(&stIspRegister.stSnsExp);
    s32Ret = HI_MPI_ISP_SensorRegCallBack(IspDev, OV9712_ID, &stIspRegister);
    if (s32Ret)
    {
        printf("sensor register callback function failed!\n");
        return s32Ret;
    }
    
    stLib.s32Id = 0;
    strncpy(stLib.acLibName, HI_AE_LIB_NAME, sizeof(HI_AE_LIB_NAME));
    cmos_init_ae_exp_function(&stAeRegister.stSnsExp);
    s32Ret = HI_MPI_AE_SensorRegCallBack(IspDev, &stLib, OV9712_ID, &stAeRegister);
    if (s32Ret)
    {
        printf("sensor register callback function to ae lib failed!\n");
        return s32Ret;
    }

    stLib.s32Id = 0;
    strncpy(stLib.acLibName, HI_AWB_LIB_NAME, sizeof(HI_AWB_LIB_NAME));
    cmos_init_awb_exp_function(&stAwbRegister.stSnsExp);
    s32Ret = HI_MPI_AWB_SensorRegCallBack(IspDev, &stLib, OV9712_ID, &stAwbRegister);
    if (s32Ret)
    {
        printf("sensor register callback function to ae lib failed!\n");
        return s32Ret;
    }
    
    return 0;
}

int sensor_unregister_callback(void)
{
    ISP_DEV IspDev = 0;
    HI_S32 s32Ret;
    ALG_LIB_S stLib;

    s32Ret = HI_MPI_ISP_SensorUnRegCallBack(IspDev, OV9712_ID);
    if (s32Ret)
    {
        printf("sensor unregister callback function failed!\n");
        return s32Ret;
    }
    
    stLib.s32Id = 0;
    strncpy(stLib.acLibName, HI_AE_LIB_NAME, sizeof(HI_AE_LIB_NAME));
    s32Ret = HI_MPI_AE_SensorUnRegCallBack(IspDev, &stLib, OV9712_ID);
    if (s32Ret)
    {
        printf("sensor unregister callback function to ae lib failed!\n");
        return s32Ret;
    }

    stLib.s32Id = 0;
    strncpy(stLib.acLibName, HI_AWB_LIB_NAME, sizeof(HI_AWB_LIB_NAME));
    s32Ret = HI_MPI_AWB_SensorUnRegCallBack(IspDev, &stLib, OV9712_ID);
    if (s32Ret)
    {
        printf("sensor unregister callback function to ae lib failed!\n");
        return s32Ret;
    }
    
    return 0;
}

#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* End of #ifdef __cplusplus */

#endif /* __MN34220_CMOS_H_ */
